﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace FlashGames
{
  public static class Util
  {
    public static CookiesAwareWebClient WebClient = new CookiesAwareWebClient();
    public static string baseUrl;
    public static string WebData;
    public static string ResovedUrl;
    public static bool DownloadStringComplete = false;

    public static string RemoveWhiteSpaces(string Imput)
    {
      string result = Imput;

      // Remove double spaces
      result = Regex.Replace(result, @"  +", "", RegexOptions.Multiline);

      // Replace multiple newlines with just one
      result = Regex.Replace(result, @"(\r?\n)+", "\n", RegexOptions.IgnoreCase & RegexOptions.Multiline);

      // Remove whitespace at the beginning and end
      result = result.Trim();

      return result;
    }

    public static string ResolvedUrl(string Url, string BaseUrl)
    {
      if (Url == null)
      {
        return BaseUrl;
      }
      if (!Url.StartsWith("http://"))
      {
        //if (Url.StartsWith("http://"))
        //    //Url = Url.Replace("http://", "http://www.");

        if (!string.IsNullOrEmpty(BaseUrl))
        {
          //if (!BaseUrl.StartsWith("http://www."))
          //{
          //    if (!BaseUrl.StartsWith("http://"))
          //        BaseUrl = BaseUrl.Replace("http://", "http://www.");
          //}
          if (Url.StartsWith("//"))
            Url = "http:" + Url;

          else if (Url.StartsWith("/") && BaseUrl.EndsWith("/"))
            Url = BaseUrl + Url.Remove(0, 1);

          else if (!Url.Equals(BaseUrl))
            Url = BaseUrl + Url;
        }

      }


      return ResovedUrl = Url;
    }

    public static string GetWebData(string Url)
    {
      WebClient.BaseAddress = baseUrl;
      WebClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;
      WebData = WebClient.DownloadString(ResolvedUrl(Url, baseUrl));

      return WebData;
    }

    static void WebClient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
    {
      DownloadStringComplete = true;
    }

    public static string PlainTextFromHtml(string input)
    {
      string result = input;
      if (!string.IsNullOrEmpty(result))
      {
        // decode HTML escape character
        //result = System.Web.HttpUtility.HtmlDecode(result);

        // Replace &nbsp; with space
        result = Regex.Replace(result, @"&nbsp;", " ", RegexOptions.Multiline);

        // Remove double spaces
        result = Regex.Replace(result, @"  +", "", RegexOptions.Multiline);

        // Replace <br/> with \n
        result = Regex.Replace(result, @"< *br */*>", "\n", RegexOptions.IgnoreCase & RegexOptions.Multiline);

        // Remove remaining HTML tags                
        result = Regex.Replace(result, @"<[^>]*>", "", RegexOptions.Multiline);

        // Replace multiple newlines with just one
        result = Regex.Replace(result, @"(\r?\n)+", "\n", RegexOptions.IgnoreCase & RegexOptions.Multiline);

        // Remove whitespace at the beginning and end
        result = result.Trim();
      }
      return result;
    }

    public static Image GetImageFromUrl(string imageUrl)
    {
      WebClient.BaseAddress = baseUrl;
      try
      {
        Image image = Bitmap.FromStream(WebClient.OpenRead(imageUrl));
        return image;
      }
      catch { }
      return null;
    }

    public static Dictionary<string, string> RegexGroupOptions()
    {
      Dictionary<string, string> ListItems = new Dictionary<string, string>();
      ListItems.Add("Url", "(?<url>[^@]*)");
      ListItems.Add("Title", "(?<title>[^@]*)");
      ListItems.Add("ImageUrl", "(?<imageUrl>[^@]*)");
      ListItems.Add("Description", "(?<description>[^@]*)");
      ListItems.Add("File", "(?<file>[^@]*)");
      ListItems.Add("Skip to char", "[^@]*");
      ListItems.Add("skip to", "(?:(?!@).)*");
      ListItems.Add("Match Before", "(?<!@@.*)");
      ListItems.Add("Match After", "(?<=@@.*)");
      ListItems.Add("Match Not", "(?!@@)");
      return ListItems;
    }

    public static string GetThumbPath()
    {
      string path = @"C:\ProgramData\Team MediaPortal\MediaPortal\thumbs\FlashGames\";
      if (!File.Exists(path))
        Directory.CreateDirectory(path);
      return path;
    }

    public static void DownloadThumb(string Url, string fileName)
    {
      try
      {
        Uri uri = new Uri(Url);
        string FilePath = GetThumbPath() + fileName + Url.Substring(Url.LastIndexOf("."));
        if (!File.Exists(FilePath))
          WebClient.DownloadFile(uri, FilePath);
      }
      catch { }
    }

    public static Image GetThumbFormCache(string fileName)
    {
      Image image = Bitmap.FromFile(GetThumbPath() + fileName);

      return image;
    }
  }
}
